layui.use(['form', 'layer', 'table', 'upload'], function () {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : top.layer,
		$ = layui.jquery,
		table = layui.table,
		upload = layui.upload,
		company_id = sessionStorage["company_id"],
		user_id = sessionStorage["userId"],
		scale = sessionStorage["scale"];

	if (user_id == 3) {
		loadCompanyList(form, $);
		$('#manager').removeClass("layui-hide");
		$('.company').removeClass("layui-hide");
	}
	if (scale == 1) {
		$('#manager').removeClass("layui-hide");
	}

	function uploadRender(type) {
		upload.render({
			elem: `#upload${type}`,
			url: `${baseUrl}file/upload.php?type=${type}&company_id=${company_id}`, //此处配置你自己的上传接口即可
			accept: 'file', //普通文件
			done: function (res) {
				layer.msg('上传成功');
				tableIns.reload();
			},
			error: function () {
				layer.msg('已有同名文件不能重复上传！');
			}
		});
	}
	uploadRender(0);
	uploadRender(1);

	//用户列表
	var tableIns = table.render({
		elem: '#fileList',
		url: baseUrl + 'file/getFiles.php',
		where: {
			scale,
			id: user_id,
			company_id
		},
		skin: 'row',
		even: true, //开启隔行背景
		toolbar: true,
		defaultToolbar: ['filter'],
		cellMinWidth: 95,
		page: true,
		limits: [10, 15, 20, 25],
		limit: 10,
		id: "fileListTable",
		cols: [
			[
				// {
				// 	type: "checkbox",
				// 	fixed: "left"
				// },
				{
					title: '序号',
					type: "numbers"
				},
				{
					field: 'id',
					title: 'ID',
					hide: true
				},
				{
					field: 'file_name',
					title: '文件名'
				},
				{
					field: 'file_path',
					title: '文件下载',
					templet: function (d) {
						return `<a class="download_${d.id}" style="color:blue;" href="${d.file_path}">点击下载</a>`
					}
				},
				{
					field: 'type',
					title: '合同类型',
					templet: function (d) {
						return d.type == 0 ? '公对公' : '公对私'
					}
				},
				{
					field: 'companyName',
					title: '所属公司'
				},
				{
					title: '操作',
					width: 150,
					templet: '#fileListBar',
					fixed: "right",
					align: "center"
				}
			]
		],
		done: function (res, curr, count) {// done 数据表格加载完成

		}
	});
	$('.file_name').on('keyup', function (event) {
		if (event.keyCode != 13) return
		$(".search_btn").click()
	})
	//搜索
	$(".search_btn").on("click", function () {
		table.reload("fileListTable", {
			page: {
				curr: 1 //重新从第 1 页开始
			},
			where: {
				file_name: $(".file_name").val(), //搜索的关键字
				company_id: user_id == 3 ? $("#company").val() : company_id,
				file_type: $("#file_type").val()
			}
		})
	});

	//列表操作
	table.on('tool(fileList)', function (obj) {
		const { id, file_name } = obj.data;
		var layEvent = obj.event;
		if (layEvent === 'edit') { //编辑
			editUser(data);
		} else if (layEvent === 'del') { //删除
			layer.confirm('确定删除文件 ' + file_name + ' ？', {
				icon: 3,
				title: '删除确认'
			}, function (index) {
				$.ajax({
					url: baseUrl + "file/deleteFile.php",
					data: {
						id,
						file_name
					},
					type: "POST",
					dataType: "text",
					success: function (data) {
						if (data == "1") {
							tableIns.reload();
							layer.close(index);
							layer.msg('删除文件成功！')
						} else {
							layer.msg('删除文件失败！')
						}
					}
				})
			});
		}
	});

	table.on('rowDouble(fileList)', function (obj) {
		document.querySelector(`.download_${obj.data.id}`).click();
	});
})